﻿var NEXT_ACTION_NEW = '1'; // 继续新建
var NEXT_ACTION_EDIT = '2'; // 继续编辑
var NEXT_ACTION_TO_LIST = '3'; // 返回列表

function savePost() {
    var datas = [];
    window.editor.sync();

    $('form').each(function (evt) {
        var $this = $(this);
        var data = $this.serializeArray();
        $.merge(datas, data);
    });

    var next_action = $('#next_action').val();

    $.post('/post/save', datas).success(function (res) {
        layer.msg("保存成功", { icon: 1 });
        switch (next_action) {
            case NEXT_ACTION_NEW:
                //$('form').each(function () {
                //    $(this).trigger('reset')
                //});
                //$('#coverImageEl').html('');
                //$('#detail').val('');
                //window.editor.html('');
                window.location.href = '/post/edit?next_action=' + next_action;
                break;
            case NEXT_ACTION_EDIT:
                window.location.href = '/post/edit/' + res.ID + '?next_action=' + next_action;
                break;
            case NEXT_ACTION_TO_LIST:
                window.location.href = '/post/list';
                break;
        }
    }).error(function () {
        layer.msg("保存失败", { icon: 1 });
    });
}


function uploadFile(file) {
    new FileUpload({
        url: '/image',
        fieldName: 'fileData'
    }, file.files[0]).upload(function (res) {
        $('#FirstPicture').val(res.Path);
        $('#coverImageEl').html('<img class="cover" src="{0}" />'.replace('{0}', res.PictureURL));
    });
}


function editCoverPicture() {
    var coverPicurePath = $('#FirstPicture').val();
    if (!coverPicurePath) {
        layer.msg('请先上传封面图片');
        return;
    }

    var index = layer.open({
        type: 2,
        content: '/image/edit?path=' + coverPicurePath,
        area: ['100%', '100%'],
        maxmin: true
    });
    layer.full(index);
}

function editPictureCallBack(picturePath) {
    layer.closeAll();
    $('#FirstPicture').val(picturePath);
    $('#coverImageEl').html('<img class="cover" src="/image?path={0}" />'.replace('{0}', picturePath));
}


var category_vm = avalon.define({
    $id: 'category',
    categories: [],
    categories_data: null, // 编辑时的文章类目数据
    fetch_category: function () {
        $.get('/category').success(function (res) {
            category_vm.categories = res.Datas;
            category_vm.render_categories_data();
        });
    },
    render_categories_data: function () {
        if (!this.categories_data) this.categories_data = JSON.parse($('#category_data').val()) || [];
        for (var i = 0, len = this.categories.length; i < len; i++) {
            var category = this.categories[i];

            for (var j = 0, len2 = this.categories_data.length; j < len2; j++) {
                var category_data = this.categories_data[j];

                if (category.ID === category_data.ID) {
                    category.is_checked = true;
                }
            }
        }
        console.log(this.categories_data);
    }
});
category_vm.fetch_category();